const conf = {
  data: {
    // hasEmptyGrid 变量控制是否渲染空格子，若当月第一天是星期天，就不应该渲染空格子
    hasEmptyGrid: false ,
    haslast:false,
    tourist_num:{
      elder:0,
      adult:0,
      children:0,
      sum:0
    },
    a_day:[31,15,25,21,4],
    select_day:[],
    thisday:[],
  },
  navto_order(){
    var date = this.data.cur_year+'-'+this.data.cur_month+'-'+this.data.thisday.select;
    var num = this.data.tourist_num.sum;
    wx.navigateTo({
      url: '../../pages/cost_detail/cost_detail?date='+date+"&price=￥4564"+"&num="+num,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  //控制选中状态和选中日期
  select_this(e){
    let index = e.target.dataset.i;
    let this_day = this.data.thisday[index]
    let thisday = [];
    thisday[index] = !this_day;
    if(thisday[index]){
      thisday["select"] = index;
    }
    
    this.setData({
      thisday
    })
    console.log(this.data.thisday)
  },
  //控制选择的人数、以及分类
  touristchange(e){
    var cdata = this.data.tourist_num;
    if(e.target.dataset.elder){
     //老人
        if(e.target.dataset.elder==2){
          cdata.elder--
          if(cdata.elder<0){
            cdata.elder=0
          }else{
            cdata.sum--
          }
        }else{
          cdata.elder++
          cdata.sum++
        }
    }else if(e.target.dataset.adult){
      //成人
      if(e.target.dataset.adult==2){
          cdata.adult--
          if(cdata.adult<0){
            cdata.adult=0
          }else{
            cdata.sum--
          }
        }else{
          cdata.adult++
          cdata.sum++
        }
    }else if(e.target.dataset.children){
      //儿童
        if(e.target.dataset.children==2){
          cdata.children--
          if(cdata.children<0){
            cdata.children=0
          }else{
            cdata.sum--
          }
        }else{
          cdata.children++
          cdata.sum++
        }
    }
    this.setData({
      tourist_num:cdata
    })
  },
  getSystemInfo() {
    try {
      const res = wx.getSystemInfoSync();
      this.setData({
        scrollViewHeight: res.windowHeight * res.pixelRatio || 667
      });
    } catch (e) {
      console.log(e);
    }
  },
  //小日历开始                                                、、小日历开始
  // 获取当月共多少天
  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  // 获取当月第一天星期几
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  getLastDayOfWeek(year, month){
    var days = this.getThisMonthDays(year, month);
    console.log(days)
    return new Date(Date.UTC(year, month - 1, days)).getDay();
  },
  // 计算当月1号前空了几个格子
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },
  //计算当月最后一天空的格子数量
  computelast(year, month){
    var lastday = this.getLastDayOfWeek(year, month);
    var last = [];
    if(lastday!=6){
      for(let i=0;i<6-lastday;i++){
        last.push(i);
      }
      this.setData({
        haslast:true,
        last:last
      })
    }else{
      this.setData({
         haslast:false,
          last:[]
      })
    }
  },
  // 绘制当月天数占的格子
  calculateDays(year, month) {
    let days = [];
    const thisMonthDays = this.getThisMonthDays(year, month);
    //渲染可以选择的日期
    let select_day = this.data.select_day;
    let a_day = this.data.a_day;
    for (let i = 1; i <= thisMonthDays; i++) {
      days.push(i);
      for(let j=0;j<a_day.length;j++){
        if(i==a_day[j]){
          select_day[i]=true
          break
        }else{
          select_day[i] = false
        }
      }
    }
    //结束  渲染可以选择的日期
    this.setData({
      days,
      select_day
    });
    
  },
  // 初始化数据
  onLoad(options) {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.computelast(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.getSystemInfo();
    this.setData({
      cur_year,
      cur_month,
      weeks_ch
    })
  },
  // 切换控制年月
  handleCalendar(e) {
    const handle = e.currentTarget.dataset.handle;
    const cur_year = this.data.cur_year;
    const cur_month = this.data.cur_month;
    
    if (handle === 'prev') {
      let newMonth = cur_month - 1;
      let newYear = cur_year;
      if (newMonth < 1) {
        newYear = cur_year - 1;
        newMonth = 12;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);
      this.computelast(newYear, newMonth);
      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })

    } else {
      let newMonth = cur_month + 1;
      let newYear = cur_year;
      if (newMonth > 12) {
        newYear = cur_year + 1;
        newMonth = 1;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);
      this.computelast(newYear, newMonth);
      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })
    }
  },
  //日历结束  、、、             结束
};

Page(conf);